<?php
namespace addons\wanlshop\library\WanlPay;

class WanlRefundDo
{
    // 红积分抵扣消费券处理
    public static function doRed($user_id,$order_id){
        $findRedWhere['order_user_id']=$user_id;
        $findRedWhere['order_id']=$order_id;
        $do_red_arr=model('app\common\model\RedPointsLog')->where($findRedWhere)->select();
        if($do_red_arr){
            $consumData=[];
            $PidRedPointsLog=[];
            foreach($do_red_arr as $k => $do_red){
                $red_points_do= abs($do_red['points']);//需处理的红积分

                // 查询现在用户数值
                $do_user_now=model('app\common\model\User')->field('red_points,coupon_num')->where('id',$do_red['user_id'])->find();
                $do_red_points_now=$do_user_now['red_points'];//现在红积分数值
                $do_coupon_num_now=$do_user_now['coupon_num'];//现在消费券数值

                if($do_red['user_id']==$user_id){//自己处理
                    
                    $PidRedPointsLog[$k]['get_type']=3;//获得或使用方式：1自己下单加2粉丝下单减3自己下单退款减4粉丝下单退款加                 
                    
                    // 给自己扣除红积分
                    model('app\common\model\User')->where('id',$user_id)->dec('red_points',$red_points_do)->update();
                }else{//其他人处理
                    if(!isset($coupon_num_do)){//需处理的消费券
                        $findCardWhere['order_id']=$order_id;
                        $findCardWhere['user_id']=$do_red['user_id'];
                        $findCardWhere['get_type']=1;
                        $coupon_num_do=model('app\common\model\ConsumCard')->where($findCardWhere)->value('money');
                    }
                  
                    $PidRedPointsLog[$k]['get_type']=4;//获得或使用方式：1自己下单加2粉丝下单减3自己下单退款减4粉丝下单退款加
                
                    // 抵用券
                    $consumData[$k]['user_id']=$do_red['user_id'];
                    $consumData[$k]['money']='-'.$do_red['money'];
                    $consumData[$k]['balance']=$do_coupon_num_now-$do_red['money'];
                    $consumData[$k]['order_id']=$do_red['order_id'];
                    $consumData[$k]['order_no']=$do_red['order_no'];
                    $consumData[$k]['order_user_id']=$do_red['order_user_id'];
                    $consumData[$k]['order_shop_id']=$do_red['order_shop_id'];
                    $consumData[$k]['get_type']=5;//获得或使用方式：1红积分兑换2绿积分3消费使用4全国运营中心5红积分退款6全国运营中心退款
                
                    // 给其他人返还红积分，扣除消费券
                    model('app\common\model\User')->where('id',$do_red['user_id'])->inc('red_points',$red_points_do)->dec('coupon_num',$coupon_num_do)->update();
                }
               
                // 积分
                $PidRedPointsLog[$k]['user_id']=$do_red['user_id'];
                $PidRedPointsLog[$k]['points']='-'.$do_red['points'];
                $PidRedPointsLog[$k]['balance']= $do_red_points_now-$do_red['points'];
                $PidRedPointsLog[$k]['order_id']=$do_red['order_id'];
                $PidRedPointsLog[$k]['order_no']=$do_red['order_no'];
                $PidRedPointsLog[$k]['order_user_id']=$do_red['order_user_id'];
                $PidRedPointsLog[$k]['order_shop_id']=$do_red['order_shop_id'];
            } 

            if($consumData){
                // 写入消费券日志
                model('app\common\model\ConsumCard')->saveAll($consumData);
            }

            if($PidRedPointsLog){
                // 写入红积分发放日志
                model('app\common\model\RedPointsLog')->isUpdate(false)->saveAll($PidRedPointsLog);
            }
        }        
    }

    // VIP相关处理
    public static function doVip($user_id){
        $self_user=model('app\common\model\User')->field('level,total_consum_price')->where('id',$user_id)->find();

        if($self_user['level']==2&&($self_user['total_consum_price']<350)){
            model('app\common\model\User')->where('id',$user_id)->update(['level'=>1]);
        }
    }

    // 星推官相关处理
    public static function doStarReference($user_id,$order_id){
        $starUserWhere['order_user_id']=$user_id;
        $starUserWhere['order_id']=$order_id;
        $starUserWhere['get_type']=1;
        $find_commission=model('app\common\model\ConsumCommission')->where($starUserWhere)->find();

        if($find_commission){//有星推官
            // 查询现在用户数值
            $star_user_now=model('app\common\model\User')->field('id,commission')->where('id',$find_commission['user_id'])->find();
            
            // 给星推官扣除佣金
            model('app\common\model\User')->where('id',$find_commission['user_id'])->dec('commission',$find_commission['money'])->update();
            
            // 写入佣金日志表
            $CommissionData['user_id']=$find_commission['user_id'];
            $CommissionData['money']='-'.$find_commission['money'];
            $CommissionData['balance']=$star_user_now['commission']-$find_commission['money'];
            $CommissionData['get_type']=4;//获得或使用方式：1星推官佣金2区县服务商3提现4星推广退款5区县服务商退款
            $CommissionData['order_id']=$find_commission['order_id'];
            $CommissionData['order_no']=$find_commission['order_no'];
            $CommissionData['order_user_id']=$find_commission['order_user_id'];
            $CommissionData['order_shop_id']=$find_commission['order_shop_id'];
            model('app\common\model\ConsumCommission')->insert($CommissionData);
        }
    }

    // 区县服务商相关处理
    public static function doArea($user_id,$order_id){
        $areaUserWhere['order_user_id']=$user_id;
        $areaUserWhere['order_id']=$order_id;
        $areaUserWhere['get_type']=2;
        $find_area=model('app\common\model\ConsumCommission')->where($areaUserWhere)->find();
        
        if($find_area){//有区县服务商
            // 查询现在用户数值
            $area_user_now=model('app\common\model\User')->field('id,commission')->where('id',$find_area['user_id'])->find();
            
            // 给区县服务商扣除佣金
            model('app\common\model\User')->where('id',$find_area['user_id'])->dec('commission',$find_area['money'])->update();
            
            // 写入佣金日志表
            $CommissionData['user_id']=$find_area['user_id'];
            $CommissionData['money']='-'.$find_area['money'];
            $CommissionData['balance']=$area_user_now['commission']-$find_area['id'];
            $CommissionData['get_type']=5;//获得或使用方式：1星推官佣金2区县服务商3提现4星推广退款5区县服务商退款
            $CommissionData['order_id']=$find_area['order_id'];
            $CommissionData['order_no']=$find_area['order_no'];
            $CommissionData['order_user_id']=$find_area['order_user_id'];
            $CommissionData['order_shop_id']=$find_area['order_shop_id'];
            model('app\common\model\ConsumCommission')->insert($CommissionData);
        }
    }

    // 全国运营中心相关处理
    public static function doNational($user_id,$order_id){
        $nationalUserWhere['order_user_id']=$user_id;
        $nationalUserWhere['order_id']=$order_id;
        $nationalUserWhere['get_type']=4;
        $find_national=model('app\common\model\ConsumCard')->where($nationalUserWhere)->find();

        if($find_national){//有全国运营中心
            // 查询现在用户数值
            $national_user_now=model('app\common\model\User')->field('id,coupon_num')->where('id',$find_national['user_id'])->find();
          
            // 给全国运营中心扣除消费券
            model('app\common\model\User')->where('id',$find_national['user_id'])->dec('coupon_num',$find_national['money'])->update();

            // 写入消费券日志表
            $consumData['user_id']=$find_national['user_id'];
            $consumData['money']='-'.$find_national['money'];
            $consumData['balance']=$national_user_now['coupon_num']-$find_national['money'];
            $consumData['order_id']=$find_national['user_id'];
            $consumData['order_no']=$find_national['user_id'];
            $consumData['order_user_id']=$find_national['user_id'];
            $consumData['order_shop_id']=$find_national['user_id'];
            $consumData['get_type']=6;//获得或使用方式：1红积分兑换2绿积分3消费使用4全国运营中心5红积分退款6全国运营中心退款
            model('app\common\model\ConsumCard')->insert($consumData);
        }
           
    }
}